﻿using Dapper;
using DevExpress.ReportServer.ServiceModel.ConnectionProviders;
using Microsoft.Data.Sqlite;
using Volo.Abp.DependencyInjection;
using YAKEJI.Abp.Database;
using YAPACS.BusinessObjects.Exams;
using YAPACS.RisServer.Abstractions;
using YAPACS.Workflows;

namespace YAPACS.RisServer.Workflows.Register;

public class Refund(IExamEventService examEventService, IDatabaseConnectionProvider connectionProvider) : WorkflowBase, IRefund, ITransientDependency
{
    private readonly IExamEventService _examEventService = examEventService;
    private readonly IDatabaseConnectionProvider _connectionProvider = connectionProvider;

    protected override async Task<WorkflowOutput> InvokeCoreAsync(WorkflowInput input)
    {
        return new WorkflowOutput { Success = true };
        const string key = "Exams";
        if (input.ContainsKey(key) && input.GetValueOrDefault(key) is List<Exam> exams)
        {
            if (exams.Any(e => e.InterfaceService == null))
            {
                var list = exams.Where(e => e.InterfaceService == null).ToList();
                //不是第三方的数据
                throw new Exception(string.Join(',', list.Select(e => e.AccessionNumber)) + "不是第三方数据，不能退费");
            }
            //调用接口等一系列操作
            foreach (var exam in exams)
            {
                switch (exam.InterfaceService.Name)
                {
                    default:
                        await RefundAsync(exam);
                        break;
                }
            }
            return new WorkflowOutput
            {
                Success = true,
                Info = "退费成功"
            };
        }
        return new WorkflowOutput
        {
            Success = false,
            Info = "未找到数据"
        };
    }

    private async Task RefundAsync(Exam exam)
    {
        var examApplyNumbers = exam.ExamApplyItems.Select(e => e.ExamItemNumber).ToList();
        await SetExamApplies(examApplyNumbers);
        await _examEventService.AddExamEventAsync(exam, "申请退费");
    }

    private async Task SetExamApplies(List<string> examApplyNumbers)
    {
        string dbFile = Path.Combine(@"c:\yapacs\mock-data", @"ExamApply.db");
        string connectionString = $@"Data Source={dbFile};";
        using var connection = new SqliteConnection(connectionString);
        SQLitePCL.Batteries.Init();
        string sql = @"update ExamApply set IsRefund=1 where ExamItemNumber in :examApplyNumbers";
        await connection.ExecuteAsync(sql, new { examApplyNumbers });
    }

    private async Task SetExamAppliesProc(List<string> examApplyNumbers)
    {
        const string connectionString = @"Server=192.168.2.82;Initial Catalog=HIS;User Id=sa;Password=pacs@1234;TrustServerCertificate=True";
        using var connection = _connectionProvider.GetDbConnection(connectionString, DatabaseType.SqlServer);
        await connection.ExecuteAsync("pacs_proc_exam_refund", new { examApplyNumbers }, commandType: System.Data.CommandType.StoredProcedure);
    }
}
